package com.login;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.dbConnector.DataBaseConnector;

public class AuthenticationFilterForLogin  extends DataBaseConnector {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response){
		
		RequestDispatcher rd = request.getRequestDispatcher("/anasayfa/index.jsp");
		try {
			rd.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	ResourceBundle resource = ResourceBundle.getBundle("properties.bundle.Messages_tr");				
	
	String username;
	String password;
	
	String validationMessage;
	
	HttpServletRequest request = null;
	HttpServletResponse response=null;
	
	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		
		this.request=request;
		this.response=response;
		
		response.setContentType("text/html;charset=UTF-8");
		
		try{
		    username =(String) request.getParameter("userId");		   
			password =(String) request.getParameter("passwordId");
			
			String logOut =(String) request.getParameter("logoutId");
		    String logIn = request.getParameter("loginId");
											 	
        if(logIn!=null && logIn.equalsIgnoreCase("Login")){
        	        	   
        	  if(!validate())
       		     return;
        		  
        	        	   
		   if(authenticationQuery(username,password)){
				  
			    HttpSession session = request.getSession(true);
					  
				session.setAttribute("username", username);
				session.setAttribute("password", password); 
				
				return;	
				
		   }else{
			   validationMessage= resource.getString("username.and.password");	   
			   request.setAttribute("message", validationMessage);
			   
			    return;
		   }
			 	  	 		  
         }else if(logOut!=null && logOut.equalsIgnoreCase("Logout")){
      	 
      	   HttpSession session=request.getSession(false);      	        
      	   session.invalidate();
      	        
     	        return;
          }
                         
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {					
				// response.sendRedirect("/e_ticaret/anasayfa/index.jsp");															
				
				RequestDispatcher rd = request.getRequestDispatcher("/anasayfa/index.jsp");
				rd.forward(request, response);
				
			
				
				//request.getRequestDispatcher("/anasayfa/index.jsp").forward(request, response);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}		
	}
	
	
	 public boolean authenticationQuery(String username,String password) throws Exception{
	    		 
	    	String selectSQL = "SELECT * FROM kullanici where User=? and Password=?";
	    	 
	    	PreparedStatement	preparedStatement=null;
	    	
	    	boolean resultBoolean=false;
	    	
			try {
				getDBConnection();
				
				preparedStatement = getDbConnection().prepareStatement(selectSQL);
				preparedStatement.setString(1, username);
				preparedStatement.setString(2, password);
				
				ResultSet resultSet = preparedStatement.executeQuery();				
					
				if(resultSet.next()){
					resultBoolean=true;
				}
				
				
			} catch (SQLException e) {
				e.printStackTrace();
	 
			} finally {
	 
				if (preparedStatement != null) {
					preparedStatement.close();
				}
	 
				if (getDbConnection() != null) {
					getDbConnection().close();
				}	 
			}
			
	    	return resultBoolean;
	    }
	 
	 
	 
	 
	 
   public boolean validate() throws Exception{	   
	   if(StringUtils.isBlank(username)){	   
		   
		   validationMessage= resource.getString("username.and.password");	   
		   request.setAttribute("message", validationMessage);
		   return false;		   
	   
	   }else if(!username.contains("@")){		   
		   validationMessage= resource.getString("username.and.password");	   
		   request.setAttribute("message", validationMessage);
		   return false; 
	   }
	   else if(StringUtils.isBlank(password)){
		   
		   validationMessage= resource.getString("username.and.password");	   
		   request.setAttribute("message", validationMessage);
		   return false;
	   }
	   
	   
	    return true;
   }
	
	public String getValidationMessage() {
		return validationMessage;
	}
	
	public void setValidationMessage(String validationMessage) {
		this.validationMessage = validationMessage;
	}
	
	

}
